﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace EzDesk.Utility.Web.Extensions
{

    public static class HtmlStringMethodExt
    {
        /// <summary>
        /// 判断字符串是否为空
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }

        /// <summary>
        /// 清除字符串中包含的HTML代码
        /// </summary>
        /// <param name="htmlstring"></param>
        /// <returns></returns>
        public static string RemoveHtml(this string htmlstring, bool isRemoveWhiteSpace = false)
        {
            //删除脚本
            htmlstring = htmlstring.Replace("\r\n", "");
            htmlstring = Regex.Replace(htmlstring, @"<script.*?</script>", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"<style.*?</style>", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"<.*?>", "", RegexOptions.IgnoreCase);
            //删除HTML
            htmlstring = Regex.Replace(htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"\&[^;]*;", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            htmlstring = htmlstring.Replace("<", "");
            htmlstring = htmlstring.Replace(">", "");

            if (isRemoveWhiteSpace)
            {
                htmlstring = htmlstring.RemoveSpace();
            }

            return htmlstring;
        }

        //去点空格和换行
        public static string RemoveSpace(this String msgText)
        {
            msgText = msgText.Replace("&nbsp;", "");
            msgText = msgText.Replace(" ", "");
            msgText = msgText.Replace("\r\n", "");

            return msgText;
        }

        /// <summary>
        /// 将字符串中的 Html 进行编码处理
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string HtmlEncode(this String text)
        {
            return HttpUtility.HtmlEncode(text);
        }

        /// <summary>
        /// 将字符串中的 Html 进行解码处理
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string HtmlDecode(this String text)
        {
            return HttpUtility.HtmlDecode(text);
        }
        
        /// <summary>
        /// 将字符串裁剪成某个长度，裁剪后在最后加上"..."
        /// </summary>
        /// <param name="str"></param>
        /// <param name="count"></param>
        /// <lastReview>2009.11.20</lastReview>
        /// <returns></returns>
        public static string CutString(this String str, int count)
        {
            //特殊情况，裁剪到非常短
            if (count < 3)
            {
                return "...";
            };
            if (str.Length > count)
            {
                str = str.Substring(0, count - 3) + "...";
            }
            return str;
        }

        public static string SummrizeText(this string str, int totalLength, bool autoRemoveHtml = true)
        {
            if (autoRemoveHtml)
            {
                str = str.RemoveHtml();
            }

            // 保证缩减之后 前三个字符 后三个字符而且长度足够
            if (str.Length < 6 || totalLength < 9 || str.Length <= totalLength)
            {
                return str;
            }
            return string.Format(
                "{0}...{1}",
                str.Substring(0, totalLength - 6),
                str.Substring(str.Length - 3, 3)
                );
        }





    }

}